import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashSet;
import java.util.PriorityQueue;
import java.util.concurrent.CompletionService;
import java.util.function.Consumer;
import java.util.*;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * Date: 2024-02-23
 * Time:13:29
 */
public class Solution {
    interface NoParameterNoReturn {
        void test();
        default void test2() {
            System.out.println("JDK1.8新特性，default默认方法可以有具体的实现");
        }
    }

    public static void main1(String[] args) {
        char[] ch = new char[]{'a', 'b', 'c'};
        String s1 = new String(ch); // s1对象并不在常量池中
        //s1.intern(); // s1.intern()；调用之后，会将s1对象的引用放入到常量池中
        String s2 = "abc"; // "abc" 在常量池中存在了，s2创建时直接用常量池中"abc"的引用
        System.out.println(s1 == s2);
    }

    public static void main2(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
        queue.offer(1);
        queue.offer(4);
        queue.offer(3);
        queue.offer(2);
        System.out.println(queue);
    }

    public static void main3(String[] args) {
        PriorityQueue<Integer> queue = new PriorityQueue<>(new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
    }

    public static void main4(String[] args) {
        int size = 10;
        //size = 100;  不能修改 size ，否则会报错
        NoParameterNoReturn noParameterNoReturn = ()-> System.out.println("重写了test方法！"+size);
        noParameterNoReturn.test();
        //size = 100;  也不能在这里修改它
    }

    public static void main5(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.forEach(new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                System.out.print(integer + " ");
            }
        });
    }

    ///////////////////////////////////////////////
    public boolean isSubsequence (String S, String T) {
        if (S.equals(T)) return true;
        for (int i = 0, k = 0; i < S.length(); i++, k++) {
            char ch1 = S.charAt(i);
            char ch2 = T.charAt(k);
            if (ch1 != ch2) {
                int flag = 1;  //判断 T 是否走完了
                for (; k < T.length(); k++) {
                    char ch = T.charAt(k);
                    if (ch == ch1) {
                        flag = 0;
                        break;
                    }
                }
                if (flag == 1) return false;
            }
        }
        return true;
    }
    //////////////////////////////////////////////
    public static void main6(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String str = in.nextLine();
        str = deleteSame(str);
        //if(str.equals(" ")) System.out.println(0);
        if(str.isEmpty()) System.out.println(0);
        else {
            for (int i = 0; i < str.length(); i++) System.out.print(str.charAt(i));
        }

    }

    public static String deleteSame(String s) {
/*   //法一
        Stack<Character> st = new Stack<>();
        st.push(s.charAt(0));
        for(int i = 1;i < s.length();i++) {
            char ch = s.charAt(i);
            if(st.peek() != ch) st.push(ch);
            else st.pop();
        }
        StringBuilder sb = new StringBuilder();
        while(!st.isEmpty()) sb.append(st.pop());
        sb.reverse();
        return sb.toString();
*/   //法二
        StringBuilder sb = new StringBuilder();
        int size = -1;
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (sb.length() == 0 || sb.charAt(size) != ch) {
                sb.append(ch);
                ++size;
            } else {
                sb.deleteCharAt(size--);
            }
        }
        return sb.toString();
    }
    //////////////////////////////////////
    public static boolean isSubsequence1(String s, String t) {
        if(t.equals(s)) return true;
        if(t.isEmpty()) return false;
        if(s.isEmpty()) return true;
        int j = 0;
        for(int i = 0;i < t.length();) {
            if(t.charAt(i) == s.charAt(j)) {
                i++;
                j++;
            } else i++;
        }
        if(j == s.length()) return true;
        return false;
    }

    public static void main(String[] args) {
        String s = "bcd";
        String t = "ubcd";
        System.out.println(isSubsequence1(s, t));
    }



}
